package com.mz.mattress.client.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mz.mattress.api.entity.Order;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface OrderMapper extends BaseMapper<Order> {

    @Select("select * from mz_order where order_id=#{orderId} and deleted=0 limit 1")
    Order selectByOrderId(@Param("orderId") Long orderId);

    Page<Order> listPage(Page page,
                         @Param("memberId") Long memberId);

    Page<Order> useListPage(Page page, @Param("memberId") Long memberId);

    //查询可使用订单
    @Select("select * from mz_order where member_id=#{memberId} and status in (2,3) and remain>0 and deleted=0 order by id desc limit 20")
    List<Order> list(@Param("memberId") Long memberId);

    //更新剩余次数和剩余金额  消费商家ID
//    @Update("update mz_order set remain=remain-#{change},remain_money=remain*per_money,version=version+1 where id=#{id} and version=#{version}")
    @Update("update mz_order set remain=remain-#{change},version=version+1,merchant_id=#{merchantId},status=4 where id=#{id} and version=#{version}")
    Integer updateRemain(@Param("id") Integer id, @Param("change") Integer change, @Param("merchantId") Integer merchantId, @Param("version") Integer version);

    @Select("select order_id,member_id,mobile,create_time from mz_order where id>10 and id<2938 order by rand() limit 312")
    List<Order> list2();

    @Update("update mz_order set out_trade_no=#{outTradeNo} where order_id=#{orderId}")
    Integer updateOutTradeNo(@Param("orderId") Long orderId, @Param("outTradeNo") String outTradeNo);

    //    @Select("SELECT * from mz_order WHERE remain>1 and status in (2,3) and create_time>'2025-10-01'")
    // 按更新时间筛选  避免当天多笔消费 限制200单
    @Select("select order_id from mz_order WHERE remain>#{remain} and status in (2,3) and update_time>#{startTime} and update_time<#{endTime} order by rand() limit 200")
    List<Long> selectByTime(@Param("remain") Integer remain,
                            @Param("startTime") String startTime,
                            @Param("endTime") String endTime);

}
